package com.calefk.util {
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.utils.getTimer;
	
	public class Fade {
		
		public static const FADE_IN : Number   = 1;
		public static const FADE_OUT: Number   = 2;
		
		private var mGpx:Sprite;
		private var mCanvas:MovieClip;
		
		private var mClassObject:Object  = null;
		private var mCallFunction:Function = null;
		
		private var mOldTime:Number;
		private var mVz:Number;
        
        private var mBackground:int;
		
		function Fade(canvas:MovieClip, background:int , width:Number = 600, height:Number = 400) {
			this.mCanvas = canvas;
            mBackground = background;
			drawRect(width , height);	
		}
		
		private function drawRect(width:Number , height:Number):void {
			mGpx = new Sprite();
			mGpx.x = 0;
			mGpx.y = 0;
			mCanvas.addChild(mGpx); 
			with (mGpx.graphics) {
				beginFill(mBackground, 100);
				lineStyle(1,mBackground, 100);
				lineTo(width, 0);
				lineTo(width, height);
				lineTo(0, height);
				lineTo(0, 0);
				endFill();
			}
			//--Because not Click Again
			mGpx.addEventListener(MouseEvent.CLICK , function(){});
			mGpx.useHandCursor = false;
			
			mGpx.x = 0;
			mGpx.y = 0;
			mGpx.alpha = 0;
		}
		
		public function startFade(type:Number, ifunction:Function, time:Number = 1000):void {
			switch(type){
				case FADE_IN:
					showFade(ifunction,time);
					break;
				case FADE_OUT:
					hiddenFade(ifunction,time);
					break;
			}
		}
		
		private function showFade(iFunction:Function, time:Number):void {
			mGpx.alpha = 0;
			mCallFunction = iFunction;
			mGpx.addEventListener(Event.ENTER_FRAME ,onEnterFrameShow, false, 0, true);
			// Get Timer
			mVz = 1/time;
			mOldTime = getTimer();
		}
		
		private function onEnterFrameShow(e:Event):void {
			var t:Number = getTimer() - mOldTime;
			mOldTime = getTimer();
			mGpx.alpha += mVz * t;
			if (mGpx.alpha >= 1) { 
				mGpx.removeEventListener(Event.ENTER_FRAME , onEnterFrameShow); 
				mCallFunction();
			}
		}
		
		private function hiddenFade(iFunction:Function , time:Number):void {
			mGpx.alpha = 1;
			mCallFunction = iFunction;
			mGpx.addEventListener(Event.ENTER_FRAME ,onEnterFrameHidden);
			// Get Timer
			mVz = 1/time;
			mOldTime = getTimer();
		}
		
		private function onEnterFrameHidden(e:Event):void {
			var t:Number = getTimer() - mOldTime;
			mOldTime = getTimer();
			mGpx.alpha -= mVz * t;
			if (mGpx.alpha <= 0) { 
				mGpx.removeEventListener(Event.ENTER_FRAME , onEnterFrameHidden); 
				// call function
				mCallFunction();
			}	
		}
		
		public function clean():void {
            // remove all listeners
            if (mGpx.hasEventListener(Event.ENTER_FRAME)) {
                mGpx.removeEventListener(Event.ENTER_FRAME , onEnterFrameHidden);
                mGpx.removeEventListener(Event.ENTER_FRAME , onEnterFrameShow);
            }
            // remove objects
			mCanvas.removeChild(mGpx);
			mGpx = null;
			mCanvas = null;
			mCallFunction = null;
		}
	}
}